package cn.doitedu.doitedu_eagle.web_server.service;

import beans.RuleEventParam;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2021/3/20
 **/
public class FreemarkerEventSeq {
    public static void main(String[] args) throws Exception {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setDirectoryForTemplateLoading(new File("web_server/ftl"));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        Map root = new HashMap();

        ArrayList<RuleEventParam> eventSeqParams = new ArrayList<>();

        HashMap<String,String> propSeq1 = new HashMap<>();
        propSeq1.put("pageId","3");
        RuleEventParam pvEvent1 = new RuleEventParam("pageView",propSeq1,"2021-03-19 16:40:00","-1",1);

        HashMap<String,String> propSeq2 = new HashMap<>();
        propSeq2.put("pageId","7");
        RuleEventParam pvEvent2 = new RuleEventParam("addCart",propSeq2,"2021-03-19 16:40:00","-1",1);

        HashMap<String,String> propSeq3 = new HashMap<>();
        propSeq3.put("pageId","9");
        RuleEventParam pvEvent3 = new RuleEventParam("collect",propSeq3,"2021-03-19 16:40:00","-1",1);

        eventSeqParams.add(pvEvent1);
        eventSeqParams.add(pvEvent2);
        eventSeqParams.add(pvEvent3);

        root.put("events",eventSeqParams);



        /* Get the template (uses cache internally) */
        Template temp = cfg.getTemplate("eventSequenceModel.ftl");

        /* Merge data-model with template */
        Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);
    }
}
